<?php
/**
 * Dcolors,The Community Application for Web
 *
 * Globals class
 *
 * Not free software
 * @author     chopin xiao <x@sunsne.com>
 * @copyright  2009 The Dcolors Group
 * @version    SVN: $Id: Globals.php 15 2009-01-19 11:36:08Z chopins.xiao $
 **/
abstract class Globals extends LoadFunction
{
	const INTMAX = PHP_INT_MAX;
	public $ZHNUM = array('零','一','二','三','四','五','六','七','八','九');
	public $ZHUNIT = array(null,'十','百','千','万','亿');
	public $BU = array('Byte', 'KB', 'MB', 'GB', 'TB'); 
	const WORD = '1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm';
	const UTF8 = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
	const GBK = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
	const GBK2112 = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
	const BIG5 = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
	private function getConst($name)
	{
		eval("\$con = self::$name;");
		return $con;
	}
	public function randStr($max,$min=null)
	{
		$str = null;
		$word = self::WORD;
		$len = $min == null ? $max : mt_rand($max,$min);
		for ($i=0;$i<$len;$i++)
		{
			$str .= $word{mt_rand(0,61)};
		}
		return $str;
	}
	public function getVistIp()
	{
		if(!empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
			$ip = $_SERVER['REMOTE_ADDR'];
		}else{
			$array = array('HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','REMOTE_ADDR');
			$gre = array_map('getenv',$array);
			$sre = array_map('strcasecmp',$gre);
			for($i=0;$i<3;$i++)
			{
				if($re[$i] && $sre[$i])
				{
					$ip = $re[$i];
					break;
				}
			}
		}
		preg_match("/[\d\.]{7,15}/", $ip, $ipm);
		$ip = $ipm[0] ? $ipm[0] : 'unknown';
		unset($ipm);
		return $ip;
	}
	public function getDate($type)
	{
		$type = strtolower($type);
		switch ($type) {
			case 'sec':
				return time();
			case 'mic':
				$time = explode(' ',microtime());
				return $time[1]+$time[0];
			case 'dt':
				return date('Y-m-d h:i:s');
			case 'd':
				return date('ymdhis');
			case 'n':
				$w = date('N')==7 ?'日':$this->num2zh($this->num2zh(date('N')));
				return '星期'.$w;
			case 'l':
				return date('l');
			case 'f':
				return date('Y F j');
			case 'ld':
				return date('Y-m-d' , strtotime('-1 day'));
			case 'ly':
				return date('Y-m-d' , strtotime('-1 year'));
			default:
				return date('Y-m-d');
		}
	}
	public function num2zh($num)
	{
		if(!is_numeric($num) && $num > self::INTMAX) return false;
		$num = (string)$num; 
		$zhnum = $this->ZHNUM;
		if($num<10)
		{
			return $zhnum[$num];
		}
		$zh_arr = array();
		$rnum = $this->strReverse($num);
		$zhunit = $this->ZHUNIT;
		for($i=0;$i<strlen($rnum);$i++)
		{
			$index = $i>=5 ? $i%4:$i;
			$u = $i==8?$zhunit[5]:$zhunit[$index];
			$zh_arr[] = $u;
			$zh_arr[] = $zhnum[$rnum[$i]];
		}
		 
		return implode('',array_reverse($zh_arr));
	}
	public function Bool2str($bool)
	{
		if(is_bool($bool))
		{
			return $bool ? 'TRUE' : 'FALSE';
		}else{
			return gettype($bool);
		}
	}
	public function session($name,$val=false)
	{
		if(!is_string($name)) return false;
		session_start();
		$_SESSION[$name] = empty($var) ? null: $var;
		return true;
	}
	public function cookie($name,$var=false,$expire='',$patch='/',$domain='')
	{
		if(!is_string($name)) return false;
		if(empty($var))
		{
			setcookie($name,'',time()-3600);
		}else{
			$expire = strtoupper($expire);
			switch ($expire)
			{
				case 'H':
					setcookie($name,$var,time()+3600,$patch,$domain);
				break;
				case 'D':
					setcookie($name,$var,time()+86400,$patch,$domain);
				break;
				case 'M':
					setcookie($name,$var,time()+2592000,$patch,$domain);
				break;
				case 'W':
					setcookie($name,$var,time()+604800,$patch,$domain);
				break;
				default:
					setcookie($name,$var,$expire);
				break;
			}
		}
		return true;
	}
	public function byteFormat($byte,$dec=2)
	{
		$prefix_arr = $this->BU;
		$value = round($input, $dec);
		$i=0;
		while ($value>1024)
		{
			$value /= 1024;
			$i++;
		}
		return round($value, $dec).$prefix_arr[$i]; 
	}
	public function strReverse($str,$charset=null)
	{
		if($charset == null)
		{
			$str = (string)$str;
			$j = strlen($str)-1;
			$i =0;
			while ($i<$j) {
				$tmp = $str[$i];
				$str[$i] = $str[$j];
				$str[$j] = $tmp;
				$i++;
				$j--;
			}
			return $str;
		}else{
			$charset = strtoupper($charset);
			$charset = $this->getConst($charset);
			preg_match_all($charset, $str, $match);
			return array_reverse($match);
		}
	}
	public function msubstr($str, $start=0, $length, $charset='UTF8')
	{
		if(function_exists("mb_substr"))
		{
			return mb_substr($str, $start, $length, $charset);
		}
		$charset = strtoupper($charset);
		$charset = $this->getConst($charset);
		preg_match_all($charset, $str, $match);
		return implode('',array_slice($match[0], $start, $length));
	}
	public function isPost()
	{
		if(empty($_POST))
		{
			return false;
		}else{
			return true;
		}
	}
	public function isGet()
	{
		if(empty($_GET))
		{
			return false;
		}else{
			return true;
		}
	}
}